package plugins

import (
	"encoding/json"
	"fmt"
	"io/fs"
	"os"
)

type EndPlugin struct {
	BasePlugin
}

func (self *EndPlugin) Run() (err error) {
	step := self.Steps[self.Index]
	self.Running()
	prefix := fmt.Sprintf("%03d-%s", self.Index, step.Name)

	// TODO
	err = os.WriteFile(fmt.Sprintf("%s.input.txt", prefix), []byte{}, fs.ModePerm)
	if err != nil {
		panic(err)
	}

	output := self.RenderParams()
	outputBytes, err := json.Marshal(output)
	if err != nil {
		panic(err)
	}

	err = os.WriteFile(fmt.Sprintf("%s.output.txt", prefix), outputBytes, fs.ModePerm)
	if err != nil {
		panic(err)
	}

	self.UpdateOutput(string(outputBytes))

	self.Finish()
	return nil
}
